package www.albertzhe.askForLeave.impl;

import java.util.List;

import www.albertzhe.askForLeave.Chain;
import www.albertzhe.askForLeave.Ratify;

/**
 * 实现 Chain 的真正的包装 Request 和转发功能
 */
public class RatifyChainImpl implements Chain {
    public List<Ratify> ratifyList;
    public Request request;
    public int index;

    /**
     * 
     * @param ratifyList 处理请求批准的列表
     * @param request 请求
     * @param index 已经处理过该请求的责任人数量
     */
    public RatifyChainImpl(List<Ratify> ratifyList, Request request, int index) {
        this.ratifyList = ratifyList;
        this.request = request;
        this.index = index;
    }

    @Override
    public Request request() {
        return request;
    }

    @Override
    public Result proceed(Request request) {
        Result proceed = null;
        if (ratifyList.size() > index) {
            RatifyChainImpl realChain = new RatifyChainImpl(ratifyList, request, index + 1);
            Ratify ratify = ratifyList.get(index);
            proceed = ratify.deal(realChain);
        }
        return proceed;
    }

}
